package com.csx.springcloud.invoke.resttemplate.controller;

import com.csx.springcloud.invoke.resttemplate.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

/**
 * <p> what does this class do ? </p>
 * <p>该类主要是作为 {@link RestTemplateInvokeController} 调用的目标接口的控制器</p>
 * <p> How does it work ?</p>
 * <p> Why does it do this ?</p>
 *
 * @author cuisongxu
 * @date 2023/8/3 周四 14:15
 */
@RequestMapping("/restTarget")
@Controller
public class InvokeTargetController {

    /**
     * get请求目标接口
     */
    @ResponseBody
    @GetMapping("/get")
    public String targetGet(String username) {
        return "The Response for resttemplate get request. username: " + username;
    }

    /**
     * , @RequestParam("param") String param, @RequestHeader("mock_header") String header
     *  post请求目标接口
     */

    @ResponseBody
    @PostMapping(value = "/post")
    public String targetPost(User user, @RequestHeader("mock_header") String header) {
        System.out.println(header);
        return "The Response for resttemplate post request. user: " + user;
    }

    @ResponseBody
    @PostMapping(value = "/post2")
    public String targetPost2(@RequestBody User user, @RequestParam("param") String param, @RequestHeader("mock_header") String header) {
        System.out.println(header);
        return "The Response for resttemplate post request. user: " + user + ": " + param;
    }


    @PostMapping("/postLocation")
    public String targetPostLocation(@RequestParam("param1") String param1, @RequestParam("param2") String param2) throws UnsupportedEncodingException {
        return "redirect:/success?param1=" + URLEncoder.encode(param1, StandardCharsets.UTF_8) + "&param2=" + URLEncoder.encode(param2, StandardCharsets.UTF_8);
    }

    @ResponseBody
    @RequestMapping("/success")
    public String success(String param1, String param2) {
        return "success " + param1 + ":" + param2;
    }



}
